package com.pignest.hotel.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Row;

@SuppressWarnings("unchecked")
public abstract class UIUtils {

	public static void removeAllChildren(Component c) {
		List<Component> children = c.getChildren();
		if (children == null)
			return;
		for (int i = children.size() - 1; i >= 0; i--) {
			children.get(i).detach();
		}
	}
	
	public static int getSelectedIndex(Listbox listbox) {
		if (listbox == null || listbox.getItemCount() <= 0)
			return -1;
		int index = listbox.getSelectedIndex();
		if (index == -1) {
			index = 0;
			listbox.setSelectedIndex(0);
		}
		return index;
	}
	
	public static Listitem getSelectedItem(Listbox listbox) {
		Listitem item = listbox.getSelectedItem();
		if (item == null && listbox.getItemCount() > 0) {
			listbox.setSelectedIndex(0);
			return listbox.getItemAtIndex(0);
		}
		return item;
	}
	
	public static <T> T getSelectedValue(Listbox listbox) {
		Listitem item = listbox.getSelectedItem();
		if (item == null) {
			if (listbox.getItemCount() > 0) {
				listbox.setSelectedIndex(0);
				return (T)listbox.getItemAtIndex(0).getValue();
			}
			return null;
		}
		return (T)item.getValue();
	}
	public static String getSelectedLable(Listbox listbox) {
		Listitem item = listbox.getSelectedItem();
		if (item == null) {
			if (listbox.getItemCount() > 0) {
				listbox.setSelectedIndex(0);
				return (String)listbox.getItemAtIndex(0).getLabel();
			}
			return null;
		}
		return (String)item.getLabel();
	}
	public static <T> List<T> getSelectedValues(Listbox listbox, String key) {
		boolean select = "select".equals(listbox.getMold());
		Collection<Listitem> items = listbox.getSelectedItems();
		List<T> r = new ArrayList<T>();
		if (items == null || items.isEmpty()) {
			if (select && listbox.getItemCount() > 0) {
				listbox.setSelectedIndex(0);
				Listitem item = listbox.getItemAtIndex(0);
				r.add((T) (key == null ? item.getValue() : item.getAttribute(key)));
			}
			return r;
		}
		for (Listitem item : items) {
			r.add((T) (key == null ? item.getValue() : item.getAttribute(key)));
		}
		return r;
	}
	public static <T> List<T> getSelectedCheckValues(Listbox listbox, String key) {
		boolean select = "select".equals(listbox.getMold());
		Collection<Listitem> items = listbox.getSelectedItems();
		List<T> r = new ArrayList<T>();
		if (items == null || items.isEmpty()) {
			if (select && listbox.getItemCount() > 0) {
				listbox.setSelectedIndex(0);
				Listitem item = listbox.getItemAtIndex(0);
				 r.add((T) (key == null ? item.getValue() : item.getAttribute(key)));
			}
			return r;
		}
		for (Listitem item : items) {
			if(item.isCheckable())
			  r.add((T) (key == null ? item.getValue() : item.getAttribute(key)));
		}
		return r;
	}
	/**
	 * 获得列表中先把的ID
	 * ***/
	public static String[] getSeclectListId(Listbox listbox){
		Set items = listbox.getSelectedItems();
		int count = listbox.getSelectedCount();
		String[] ids = new String[count];
		Iterator iter= items.iterator();
		int i=0;
		while(iter.hasNext()){
			Listitem item=(Listitem)iter.next();
			ids[i]=(String)item.getValue();
			i++;
		}
	
		return ids;
	}
	/**
	 * 获得选择一条记录的ID
	 * ***/
	public static String getSingleSelectListId(Listbox listbox){
		int count = listbox.getSelectedCount();
		if (count == 0) {
			return null;
		}
		return (String)listbox.getSelectedItem().getValue();
	}
	public static <T> T getSingleSelectList(Listbox listbox){
		int count = listbox.getSelectedCount();
		if (count == 0) {
			return null;
		}
		return (T)listbox.getSelectedItem().getValue();
	}
	public static void setSelectedItem(Listbox listbox, String value) {
		List<Listitem> items = listbox.getItems();
		if (items == null || items.size() == 0 || value == null)
			return;
		for (Listitem item : items) {
			if (value.equals(item.getValue())) {
				listbox.setSelectedItem(item);
				return;
			}
		}
	}
	public static void setSelectIndex(Listbox listbox,int index){
		List<Listitem> items = listbox.getItems();
		if (items == null || items.size() == 0 )
			return;
		for (Listitem item : items) {
			if (index==item.getIndex()) {
				listbox.setSelectedItem(item);
				return;
			}
		}
	}
	
	public static Row toRow(String... strs) {
		Row row = new Row();
		for (int j = 0; j < strs.length; j++) {
			Label label = new Label(strs[j]);
			row.appendChild(label);
		}
		return row;
	}
	
	public static Row toRow(Component[] cs) {
		Row row = new Row();
		for (int i = 0; i < cs.length; i++) {
			if (cs[i] == null) {
				row.appendChild(new Label());
			} else {
				row.appendChild(cs[i]);
			}
		}
		return row;
	}
	
	public static int getValue(Intbox box) {
		Integer integer = box == null ? null : box.getValue();
		return integer == null ? 0 : integer.intValue();
	}
	
	public static void alert(Object o) {
		try {
			Messagebox.show(String.valueOf(o));
		} catch (InterruptedException e) {
		}
	}
	
	public static boolean confirm(String msg) {
		try {
			if (Messagebox.show(msg, "确认", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION) == Messagebox.YES)
				return true;
		} catch (Exception e) {
		}
		return false;
	}
	
	/** 刷新页面 */
	public static void refresh() {
		Clients.evalJavaScript("window.location.href=window.location.href;");
	}
	
	public static void openWindow(String url) {
		String script = "window.open('" + url + "', 'newwindow')";
//		alert(script);
		Clients.evalJavaScript(script);
	}
	
	public static void setSelected(Radiogroup group, String value) {
		List<Component> children = group.getChildren();
		for (Component c : children) {
			if (c instanceof Radio) {
				if (((Radio)c).getValue().equals(value)) {
					((Radio)c).setSelected(true);
					return;
				}
			}
		}
	}
	
	public static void clearSelected(Component c) {
		if (c == null)
			return;
		if (c instanceof Checkbox) {
			((Checkbox)c).setChecked(false);
			return;
		}
		List<Component> cs = c.getChildren();
		for (Component component : cs) {
			if (component instanceof Checkbox) {
				((Checkbox)component).setChecked(false);
			}
		}
	}
	
	public static void addSClass(Component c, String clsName) {
		if (!(c instanceof HtmlBasedComponent))
			return;
		HtmlBasedComponent cc = (HtmlBasedComponent)c;
		String sclass = cc.getSclass();
		if (StringUtils.isEmpty(sclass)) {
			cc.setSclass(clsName);
			return;
		}
		String ss[] = StringUtils.split(sclass);
		for (int i = 0; i < ss.length; i++) {
			if (ss[i].equals(clsName))
				return;
		}
		cc.setSclass(sclass + " " + clsName);
	}
	
	public static void removeSclass(Component c, String clsName) {
		if (!(c instanceof HtmlBasedComponent))
			return;
		HtmlBasedComponent cc = (HtmlBasedComponent)c;
		String sclass = cc.getSclass();
		if (StringUtils.isEmpty(sclass)) {
			return;
		}
		if (sclass.equals(clsName)) {
			cc.setSclass(null);
			return;
		}
		StringBuilder sb = new StringBuilder();
		boolean found = false;
		String ss[] = StringUtils.split(sclass);
		for (int i = 0; i < ss.length; i++) {
			if (ss[i].equals(clsName)) {
				found = true;
				continue;
			}
			if (sb.length() > 0)
				sb.append(' ');
			sb.append(ss[i]);
		}
		if (found)
			cc.setSclass(sb.toString());
	}
	
	public static void removeAllSclass(Component c) {
		if (!(c instanceof HtmlBasedComponent))
			return;
		HtmlBasedComponent cc = (HtmlBasedComponent)c;
		cc.setSclass(null);
	}
}
